package com.zmj.dao.impl;



import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.List;

import com.zmj.dao.IOrderDao;
import com.zmj.entity.Order;
import com.zmj.entity.OrderDetail;
import com.zmj.utils.DBUtils;


public class OrderDaoImpl extends BaseDaoImpl implements IOrderDao {

	/**
	 * 添加商品详情
	 * 
	 * @param list
	 * @return
	 */
	public boolean addOrderDetails(List<OrderDetail> list) {

		try {

			// 批处理;
			String sql = "INSERT INTO t_order_detail(oid,gid,number,price,ddesc) VALUES(?,?,?,?,?)";
			Connection connection = DBUtils.getConnection();
			connection.setAutoCommit(false);
			PreparedStatement ps = connection.prepareStatement(sql);

			for (OrderDetail oe : list) {
				ps.setObject(1, oe.getOid());
				ps.setObject(2, oe.getGid());
				ps.setObject(3, oe.getNumber());
				ps.setObject(4, oe.getPrice());
				ps.setObject(5, oe.getDdesc());
				
				ps.addBatch();//添加到批处理
			}
			
			int[] results = ps.executeBatch();//执行批处理
			
			for(int i : results){
				
				if(i <= 0 ){
					connection.rollback();
					return false;
				}
			}
			
			connection.commit();
			return true;
			
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return false;
	}

	@Override
	public boolean addOrder(Order order) {
		String sql="INSERT INTO t_order(oid,oname,uid,addid,ototal,odesc) VALUES(?,?,?,?,?,?)";
		return add(sql,order.getOid(), order.getOname(),order.getUid(),order.getAddid(),order.getOtotal(),order.getOdesc());
	}

}
